﻿Imports IICBusinessLogic

''' <summary>
''' This form allows the user to add students from the StudentList window.
''' </summary>
''' <remarks></remarks>
Public Class frmAddStudents

    Public Sub setNewClassMembers(ByVal value As List(Of Student))

        Me.ucStudentList.grdStudent.DataSource = value

    End Sub

    Private Sub frmAddStudents_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Me.DesignMode Then Exit Sub

        Me.UcPeriodClassList.grdPeriodClass.gpWhereclause = "isOpen=1"
        Me.UcPeriodClassList.grdPeriodClass.loadGrid()
        Me.UcPeriodClassList.grdPeriodClass.ClearSelection()

        Me.UcPeriodClassList.grdPeriodClass.ClearSelectedRows()
        Me.ucStudentList.grdStudent.ClearSelectedRows()
        Me.ucStudentList.grdStudent.SelectionMode = DataGridViewSelectionMode.CellSelect
        Me.ShowSaveAs = False
        Me.ShowNavigationButtons = False
        Me.ShowAdd = False
        Me.ShowDelete = False

    End Sub

    Public Overrides Function SaveData() As enumSaveDataResult

        If Me.UcPeriodClassList.grdPeriodClass.SelectedRows.Count > 0 Then

            Dim db As New PeriodClassDBMapper()

            'convert the datasource of classmemberlist back to a list of classmember objects
            Dim members As List(Of Student) = CType(Me.ucStudentList.grdStudent.DataSource,  _
                                List(Of Student))

            Dim sman As IStudentClassManager = New StudentClassManagerFactory().getStudentClassManager

            For Each itm As DataGridViewRow In Me.UcPeriodClassList.grdPeriodClass.SelectedRows

                'validate that a period class exists for the selected 
                'period and lasson id selected
                Dim periodClass As PeriodClass = PeriodClassDataUtils.findByKey(CInt(itm.Cells("ClassId").Value))

                If periodClass Is Nothing Then
                    Call winUtils.MsgboxStop("There is no Class for the period and lesson you selected.")
                    Return enumSaveDataResult.SAVE_FAIL
                End If

                For Each cl As Student In members
                    Call sman.addStudentToClass(cl, periodClass)
                Next

                db.save(periodClass)

            Next

            Return enumSaveDataResult.SAVE_SUCESS_AND_CLOSE
        Else
            Call winUtils.MsgboxInfo("There are no classes selected.")

            Return enumSaveDataResult.SAVE_FAIL
        End If

    End Function

    Public Overrides Sub DeleteData()

        'nothing to do here!

    End Sub


End Class
